package net.minecraft.src;

public class EntityDragonfly extends EntityPPHFlyingCreature
{

	public EntityDragonfly(World world)
	{
		super(world);
		texture = "/pph/mob/dragonfly.png";
		setSize(1F, 0.5F);
		setMob(10F, 25);
	}
	
	public int getMaxHealth()
    {
        return 10;
    }
	
    public void writeEntityToNBT(NBTTagCompound nbttagcompound)
    {
        super.writeEntityToNBT(nbttagcompound);
    }

    public void readEntityFromNBT(NBTTagCompound nbttagcompound)
    {
        super.readEntityFromNBT(nbttagcompound);
    }

    protected String getLivingSound()
    {
        return null;
    }

    protected String getHurtSound()
    {
        return null;
    }

    protected String getDeathSound()
    {
        return null;
    }

    protected float getSoundVolume()
    {
        return 0.4F;
    }

    protected int getDropItemId()
    {
        return mod_pph.dragonflyWing.shiftedIndex;
    }
    
    public void fall(float distance)
    {
    	super.fall(distance);
    }
    
    protected void damageEntity(DamageSource source, int amount)
    {
    	if(source == DamageSource.inWall)
    		return;
    	health -= amount << 1; //Basicly amount * 2 (Bitwise left-shift)
    }
    
    protected void newX()
    {
    	if(isInjecting)
    		return;
    	if(posX > spawnX + 25D || posX < spawnX - 25D)
    	{
    		randomX = (int)spawnX;
    	}
    	else
    		super.newX();
    }
    
    protected void newZ()
    {
    	if(isInjecting)
    		return;
    	if(posZ > spawnZ + 25D || posZ < spawnZ - 25D)
    	{
    		randomZ = (int)spawnZ;
    	}
    	else
    		super.newZ();
    }
    
    protected void newY()
    {
    	if(isInjecting)
    		return;
    	super.newY();
    }
    
    protected boolean canDespawn()
    {
    	return !isInjecting; //Dont despawn during injection!
    }
    
    public void onUpdate()
    {
    	super.onUpdate();
    	if(motionY < 0)
    		wingMovement = false;
    	else
    		wingMovement = !isOnGround();
    	if(isInjecting && randomX == (int)posX && randomY == (int)posY && randomZ == (int)posZ)
    	{
    		if(ticksInjecting++ >= ticksToInject)
    		{
    			worldObj.setBlockMetadata((int)posX, (int)posY - 1, (int)posZ, 1 + rand.nextInt(3));
    			ticksInjecting = 0;
    			isInjecting = false;
    		}
    	}
    	else if(hasInjectableAlgeaBelow())
    	{
    		isInjecting = true;
    		randomX = (int)posX;
    		randomY = (int)posY - (int)distanceToSurface();
    		randomZ = (int)posZ;
    		ticksToInject = 180 + rand.nextInt(40);
    		worldObj.setBlockMetadata((int)posX, (int)randomY, (int)posZ, 1); //Prevents Double injecting
    	}
    }
    
	
	public boolean hasInjectableAlgeaBelow()
	{
		int floor = (int)posY - (int)distanceToSurface();
		if(worldObj.getBlockId((int)posX, floor, (int)posZ) == mod_pph.algae.blockID)
		{
			return worldObj.getBlockMetadata((int)posX, floor, (int)posZ) == 0;
		}
		return false;
	}
	
	public boolean interact(EntityPlayer entityplayer)
    {
        ItemStack itemstack = entityplayer.inventory.getCurrentItem();
        if(itemstack != null && itemstack.itemID == mod_pph.net.shiftedIndex)
        {
			itemstack.damageItem(1, entityplayer);
			EntityItem entityitem = new EntityItem(ModLoader.getMinecraftInstance().theWorld, (int)posX, (int)posY, (int)posZ, new ItemStack(mod_pph.dragonflyItem));
			ModLoader.getMinecraftInstance().theWorld.entityJoinedWorld(entityitem);
			setEntityDead();
			return true;
		}
		return false;
	}
    
    public boolean isFlappingWings()
    {
    	return wingMovement;
    }
    
    protected int getDropItemQuantity() 
    {
    	return 2;
    }
    
	/* Model Properties */
    boolean wingMovement = false;
    boolean isInjecting = false;
    int ticksToInject = 0;
    int ticksInjecting = 0;
	public int wingState;
	public float tailState;
	
}